package com.zzld.core.sms;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;

import org.apache.commons.codec.binary.Base64;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Response;


public class SenderSms {
	  private static final String SEND_URI = "https://sms-api.luosimao.com/v1/send.json";
	  private static final String STATUS_URI = "https://sms-api.luosimao.com/v1/status.json";
	
	  
	  public static void main(String[] args) throws Exception {
			 try {
				SenderSms.send("15652434490", "验证码：321123【汽车驿站】");
			} catch (Exception e) {
				e.printStackTrace();
			}
	   }
	  
	

	public static void send(String mobile,String message) throws Exception {
		 Map<String, String> params = new HashMap<String, String>();
	     params.put("mobile", mobile);
	     params.put("message", message);
	     AsyncHttpClient http = new AsyncHttpClient();
	        AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(SEND_URI);
	        builder.setBodyEncoding("UTF-8");
	        builder.setHeader("Authorization", "Basic " +
	        		new Base64().encodeToString("api:873cd866e41fb7e200321416a446eec3".getBytes("utf-8")));
	        if (params != null && !params.isEmpty()) {
	            Set<String> keys = params.keySet();
	            for (String key : keys) {
	                builder.addParameter(key, params.get(key));
	            }
	        }
	        Future<Response> f = builder.execute();
	        if(f.get().getStatusCode()==200){
	        	 String body = f.get().getResponseBody("UTF-8");
	        	 JSONObject result_json=JSON.parseObject(body);
	        	 if(!result_json.getString("msg").equals("ok")){
	        		 throw new Exception("查询状态异常：" + mobile + "#" +body);
	        	      
	        		 
	        	 }
	        	 System.out.println("已发送");
	        }else{
	        	throw new Exception("查询状态异常：" +f.get().getStatusCode() );
	        }
	        http.close();
	}
	
	
	@SuppressWarnings("unused")
	private static String getStatus() throws Exception {
	     AsyncHttpClient http = new AsyncHttpClient();
	        AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(STATUS_URI);
	        builder.setBodyEncoding("UTF-8");
	        builder.setHeader("Authorization", "Basic " +
	        		new Base64().encodeToString("api:873cd866e41fb7e200321416a446eec3".getBytes("utf-8")));
	        Future<Response> f = builder.execute();
	        String body=null;
	        if(f.get().getStatusCode()==200){
	        	 body = f.get().getResponseBody("UTF-8");
	        	 JSONObject result_json=JSON.parseObject(body);
	        	 if(!result_json.getString("msg").equals("ok")){
	        		 throw new Exception("发送短信异常：" +body);
	        	 }
	        }else{
	        	throw new Exception("发送短信异常：" +f.get().getStatusCode() );
	        }
	        http.close();
	        return body;
	}
	
}
